<?php
	include(dirname(__FILE__)."/../../../../../inc/class/proxy/BaseProxy.abstract.php");
?>
<?php
class WFProxy extends ABaseProxy {
	/*
	protected $cgiArgs; // all cgi parameters are stored in this associative array
	protected $errCode; // for error checking
	protected $errMessage; // for error checking (Note: only for debug, not for UI)

	protected $httpObj; // easy http communication object
	protected $serverInfo; // use this object to retrieve server information
	*/

	// Proxy Init
	public function proxy_init() {
		if( ! isset($this->cgiArgs['serverid']) &&  ! isset($this->cgiArgs['url']) ) {
			$this->errCode = WF_PROXY_ERR_INIT_INVALID_INPUT;
			$this->errMessage = WF_PROXY_ERR_INIT_INVALID_INPUT_MSG;
		}
	}

	// Proxy Exec
	public function proxy_exec() {
		$this->httpObj->setURL(urldecode($this->cgiArgs['url']));
		$this->httpObj->setGzipAutoDecode(false);
		if( $this->httpObj->Send() == FALSE ) {
			//Handle Timeout issue here
			if($this->httpObj->getErrCode()===28)
			{
				$this->errCode = WF_PROXY_ERR_EXEC_TIMEOUT;
			}
			else
			{
				$this->errCode = WF_PROXY_ERR_EXEC_CONNECT;
			}
			$this->errMessage = $this->httpObj->getErrMessage();
		}
	}

	// Proxy Result
	public function proxy_output() {
		/* build headers */
		$headers = $this->httpObj->getHeaders();
		foreach( $headers as $n => $v ) {
			// skip Set-Cookie fields
			if( $n == "Set-Cookie" || $n == "Transfer-Encoding") {
				continue;
			}
			header($n.": ".$v);
		}
		echo $this->httpObj->getBody();
	}

	// Proxy Check
	public function proxy_check() {
		if ($this->cgiArgs['serverid'] === '1')		// local
		{
			return true;
		}
		else
		{
			$this->proxy_init();
			if( $this->isFailed() ) {
				return false;
			}
			$this->proxy_exec();
			if( $this->isFailed() ) {
				return false;
			}
			return true;
		}
	}
}
?>
